package com.heima.wemedia.config;

import com.heima.model.common.constants.WmNewsMessageConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    /**
     * 上下架交换机定义
     * @return
     */
    @Bean
    public Exchange downOrUpExchange(){
        return ExchangeBuilder.topicExchange(WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC).durable(true).build();
    }

    /**
     * 上下架队列定义
     * @return
     */
    @Bean
    public Queue downOrUpQueue(){
        return QueueBuilder.durable(WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_QUEUE).build();
    }

    /**
     * 上下架交换机和队列绑定
     * @return
     */
    @Bean
    public Binding downOrUpBinding(){
        return BindingBuilder.bind(downOrUpQueue()).to(downOrUpExchange()).with(WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_ROUTINGKEY).noargs();
    }


    //死信交换机

//    /**
//     * 声明普通的队列，并为其指定死信交换机dl.direct
//     * @return
//     */
//    @Bean
//    public Queue simpleQuene(){
//        return QueueBuilder.durable("simple.queue")
//                .deadLetterExchange("dl.direct")
//                .deadLetterRoutingKey("dlsimple")
//                .build();
//    }
//
//    /**
//     * 声明普通队列交换机
//     * @return
//     */
//    @Bean
//    public DirectExchange dlExchange1(){
//        return new DirectExchange("simple.direct",true,false);
//    }
//
//
//
//    /**
//     * 声明死信交换机dl.direct
//     * @return
//     */
//    @Bean
//    public DirectExchange dlExchange2(){
//        return new DirectExchange("dl.direct",true,false);
//    }
//
//    /**
//     * 声明存储死信队列dl.queue
//     * @return
//     */
//    @Bean
//    public Queue dlQueue(){
//        return new Queue("dl.queue",true);
//    }
//
//    /**
//     * 将死信队列与死信交换机绑定
//     * @return
//     */
//    @Bean
//    public Binding dlBinding(){
//        return BindingBuilder.bind(dlQueue()).to(dlExchange2()).with("dlsimple");
//    }
//
//    /**
//     * 将普通交换机和普通队列绑定
//     * @return
//     */
//    @Bean
//    public Binding slBinding(){
//        return BindingBuilder.bind(dlQueue()).to(dlExchange1()).with("simple");
//    }

    public static final String TTL_EXCHANGE = "ttl.exchange";
    public static final String TTL_ROUTE_KEY = "ttl.key";
    public static final String TTL_QUEUE = "ttl.queue";
    @Bean
    public DirectExchange ttlExchange(){
        return new DirectExchange(TTL_EXCHANGE);
    }
    @Bean
    public Queue ttlQueue(){
        return QueueBuilder.durable(TTL_QUEUE).
                deadLetterExchange(DL_EXCHANGE).
                deadLetterRoutingKey(DL_ROUTE_KEY).
                build();
    }
    @Bean
    public Binding bindTtlQueue(){
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with(TTL_ROUTE_KEY);
    }


    public static final String DL_EXCHANGE = "dl.exchange";
    public static final String DL_ROUTE_KEY = "dl.key";
    public static final String DL_QUEUE = "dl.queue";


}
